<template>
	<view>
		<view class="">
			<uni-table ref="table" border stripe type="" emptyText="暂无更多数据" @selection-change="selectionChange">
				<uni-tr>
					<uni-th width="204" align="center">地址</uni-th>
				</uni-tr>
				<uni-tr v-for="item in addrList" :key="item.id">
					<uni-td>
						<radio :value="item.id" :checked="selectedId === item.id" @click="selectRow(item.id)">
						</radio> {{ item.province }}&nbsp;{{item.city}}&nbsp;{{item.county}}&nbsp;{{item.detail}}
						<br>
						{{item.addressName}} &nbsp;{{item.addresstel}}
					</uni-td>

					<!-- <uni-td>
					<view class="uni-group">
						<button class="uni-button" size="mini" type="primary">修改</button>
						<button class="uni-button" size="mini" type="warn">删除</button>
					</view>
				</uni-td> -->
				</uni-tr>
			</uni-table>
		</view>
		<view class="bottomBtn">
			<view class="addBtn" @click="determine">添加</view>
			<!-- <view class="addBtn" @click="">删除</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			// this.userInfo = uni.getStorageSync("userInfo");
			this.selUserAddr();
		},
		data() {
			return {
				userInfo: {},
				addrList: [],
				selectedId: null
			};
		},
		methods: {
			determine() {
				uni.navigateTo({
					url: "/pages/addUserAddress/addUserAddress"
				})
			},
			selectRow(id) {
				this.selectedId = id;
				uni.setStorageSync("addressId", this.selectedId);
				console.log(this.selectedId);
				let jwt = uni.getStorageSync('authority');
				uni.request({
					url: "api/appuser/updateUserDefaultAddr",
					header: {
						"authority": jwt
					},
					data: {
						id: this.selectedId
					},
					success: (res) => {
						// console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: "默认地址修改成功",
								icon: 'success'
							})
						}
					}
				})
			},
			selUserAddr() {
				// console.log(this.userInfo);
				let jwt = uni.getStorageSync('authority');
				uni.request({
					url: "api/appuser/selUserAddr",
					header: {
						"authority": jwt
					},
					method: 'GET',
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							this.addrList = res.data.data;
							for (var i = 0; i < res.data.data.length; i++) {
								if (res.data.data[i].defaultAddress == 1) {
									this.selectedId = res.data.data[i].id;
								}
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.bottomBtn {
		position: fixed;
		width: 300rpx;
		height: 200rpx;
		bottom: var(--window-bottom);
		right: 0;
		text-align: center;
		line-height: 100rpx;

		.addBtn {
			background-color: gray;
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			background-color: #1bb7fd;
			color: #fff;
			float: left;
			margin-right: 10rpx;
		}
	}
</style>